#include "BrickEx.h"
#include "Resource.h"


BrickEx::BrickEx(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int Object_State) : Object(x, y)
{
 
	_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\Brick_32.png"), 0, 0);
	_sprite->SetFrame(0,0);
	 
	_xOld = _x;
	_yOld = _y;

	_vx = 0;
	_vy = 0;
	_accelY = 0;
	isBreaked = false;

	isLastMoveUp = false;

	_spriteArr[0] = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\expBrick.png"), 150, 0);
	_spriteArr[1] = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\expBrick.png"), 150, 0);
	_spriteArr[2] = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\expBrick.png"), 150, 0);
	_spriteArr[3] = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\expBrick.png"), 150, 0);
	 
	//object_State = BS_NORMAL;

	object_State = Object_State;
	if (object_State == BS_NONE)
	{
		_sprite->SetFrame(1,1);
		isLastMoveUp = true;
	} 
}

BrickEx::~BrickEx()
{
	/*if (_sprite != NULL)
	{
		delete _sprite;
	}*/
}

 

void BrickEx::Render()
{
	switch(object_State)
	{
	case BS_NONE:
	case BS_NORMAL:
	case BS_MOVEUP:
		_sprite->Render(_x, _y);
		break;
	case BS_BREAKING:
		_spriteArr[0]->Render(xBreak1, yBreak1);
		_spriteArr[1]->Render(xBreak2, yBreak2);
		_spriteArr[2]->Render(2 * _x - xBreak1, yBreak1);
		_spriteArr[3]->Render(2 * _x - xBreak2, yBreak2);
		break;
	}
}

OBJECT_TYPE BrickEx::GetTypeObject()
{
	return OBJECT_LAND;
}

void BrickEx::ResetRect()
{
	_rectBond.left = _x;
	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
	_rectBond.top = _y;
	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;
} 

void BrickEx::ResetRect_NULL()
{
	_rectBond.left = 0;
	_rectBond.right = 0;
	_rectBond.top = 0;
	_rectBond.bottom = 0;
} 

void BrickEx::Update(list<Object*>* staObjs, list<Object*>* dynObjs, DxInput* input, float gameTime)
{
	 switch(object_State)
	 {
	 case BS_NONE:
		 ResetRect();
		 _sprite->Update(gameTime);
		 break;
	 case BS_NORMAL:
		 ResetRect();
		 _sprite->Update(gameTime);
		 break;
	 case BS_MOVEUP:
		 if (isLastMoveUp == false)
		 {
			 _vy += _accelY * gameTime;
			 _y += _vy * gameTime + 1/2 * _accelY * gameTime * gameTime;
			 if (_y > _yOld)
			 {
				 ChangeState(BS_NORMAL);
			 }
			 ResetRect();
			 _sprite->Update(gameTime);
		 }
		 break;
	 case BS_BREAKING:
		 xBreak1 += vxBreak1 * gameTime;
		 xBreak2 += vxBreak2 * gameTime;
		 vyBreak1 += ayBreak * gameTime;
		 vyBreak2 += ayBreak * gameTime;
		 yBreak1 += vyBreak1 * gameTime + 1/2 * ayBreak * gameTime * gameTime;
		 yBreak2 += vyBreak2 * gameTime + 1.2 * ayBreak * gameTime * gameTime;
		 if(yBreak1 > _y + 600)
		 {
			 ChangeState(BS_REMOVE);
		 }
		 _spriteArr[0]->Update(gameTime);
		 _spriteArr[1]->Update(gameTime);
		 _spriteArr[2]->Update(gameTime);
		 _spriteArr[3]->Update(gameTime);
		 ResetRect_NULL();
		 break;
	 }
}

void BrickEx::ChangeState(int state)
{
	object_State = state;
	switch (object_State)
	{
	case BS_NORMAL:
		_y = _yOld;
		_vy = 0;
		_accelY = 0;
		break;
	case BS_MOVEUP:
		_vy = -1.0;//-200;
		_accelY = GRAVITY;
		break;
	case BS_BREAKING:
		xBreak1 = xBreak2 = _x + 40/2;
		yBreak1 = _y;
		yBreak2 = _y + 40/2;
		vyBreak1 = -0.900;
		vyBreak2 = -0.600;
		vxBreak1 = 0.150;
		vxBreak2 = 0.100;
		ayBreak = GRAVITY;
		break;
	case BS_NONE:
		_sprite->SetFrame(1,1);
		isLastMoveUp = true;
		break;
	case BS_REMOVE:
		break;
	}
}